﻿using MusicForMvc3.Method;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MusicForMvc3.Controllers
{
    [AdminAuthorize]
    public class PlayListController : BaseController
    {
        public PlayListController()
        {
            base.ViewPath = "~/Views/Admin/Playlist/{0}.cshtml";
        }
        //
        // GET: /PlayList/

        public ActionResult Index()
        {
            ViewBag.List = Playlist.GetListByUserID(this.UserData.ID);

            return ViewPage();
        }

        [ActionName("modify")]
        public ActionResult Add_Index(Guid? guid = null)
        {
            if (guid != null)
            {
                var playlist = Playlist.GetByID(guid.Value);
                if (playlist != null)
                {
                    ViewBag.Guid = playlist.ID.ToString();
                    ViewBag.Key = playlist.Key;
                    ViewBag._Title = playlist.Title;
                    ViewBag.XiamiPlayUrl = playlist.XiamiPlayUrl;
                    ViewBag.XiamiPageUrl = playlist.XiamiPageUrl;
                }
            }
            return ViewPage();
        }
        [ActionName("modify")]
        [HttpPost]
        public ActionResult Add()
        {
            Html5Music.Model.PlayList mode = new Html5Music.Model.PlayList();
            mode = base.LoadPostData<Html5Music.Model.PlayList>(mode);

            bool iserror = false;
            string ErrorMsg = "";

            if (string.IsNullOrWhiteSpace(mode.Key))
            {
                iserror = true;
                ErrorMsg += "key 不能为空";
                ErrorMsg += "<br/>";
            }
            if (string.IsNullOrWhiteSpace(mode.Title))
            {
                iserror = true;
                ErrorMsg += "标题不能为空";
                ErrorMsg += "<br/>";
            }
            if (string.IsNullOrWhiteSpace(mode.XiamiPlayUrl))
            {
                iserror = true;
                ErrorMsg += "播放地址不能为空";
                ErrorMsg += "<br/>";
            }

            if (!iserror)
            {
                string guid = Request["guid"];
                Guid id;
                if (Guid.TryParse(guid, out id))
                {
                    var playitem = Playlist.GetByID(id);
                    if (playitem != null)
                    {
                        if (playitem.UserID == this.UserData.ID)
                        {
                            mode.ID = playitem.ID;
                            mode.UserID = playitem.UserID;
                            mode.Share = true;
                            var result = Playlist.Update(mode);
                            ViewBag.Result = result;
                            ViewBag.ResultMsg = "修改成功";
                            ViewBag.Redirect = "/playlist/index";
                        }
                        else
                        {
                            iserror = true;
                            ErrorMsg += "你修改的不是你的列表。";
                            ErrorMsg += "<br/>";
                        }
                    }
                    else
                    {
                        iserror = true;
                        ErrorMsg += "这不到该列表。";
                        ErrorMsg += "<br/>";
                    }
                }
                else
                {
                    mode.UserID = UserData.ID;
                    mode.Share = true;
                    var result = Playlist.Add(mode);
                    ViewBag.Result = result;
                    ViewBag.ResultMsg = "添加成功";
                    ViewBag.Redirect = "/playlist/index";
                }
            }
            if (iserror)
            {
                ViewBag.Key = mode.Key;
                ViewBag._Title = mode.Title;
                ViewBag.XiamiPlayUrl = mode.XiamiPlayUrl;
                ViewBag.XiamiPageUrl = mode.XiamiPageUrl;
                ViewBag.Error = ErrorMsg;
            }

            return Index();
        }
    }
}
